/**
 * Copyright 2012 Meruvian
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0 
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.example.yama.service.impl;

import java.util.Date;

import javax.inject.Inject;

import org.meruvian.yama.persistence.EntityListWrapper;
import org.meruvian.yama.persistence.access.PersistenceDAO;
import org.meruvian.yama.persistence.access.PersistenceManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.example.yama.dao.CategoryDAO;
import com.example.yama.entity.Category;
import com.example.yama.service.CategoryService;

/**
 * @author Dian Aditya
 * 
 */
@Service
@Transactional(readOnly = true)
public class CategoryServiceImpl extends PersistenceManager<Category> implements
		CategoryService {
	@Inject
	private CategoryDAO categoryDao;

	protected PersistenceDAO<Category> getDao() {
		return categoryDao;
	}

	public Category findCategoryById(String id) {
		return findById(id);
	}

	@Transactional
	public void saveCategory(Category category) {
		if (category.getId() != null) {
			category.getLogInformation().setUpdateDate(new Date());
		}

		save(category);
	}

	public void deleteCategory(Category category) {
		delete(categoryDao.merge(category));
	}

	public EntityListWrapper<Category> getCategoriesByName(String name,
			int limit, int page) {
		return categoryDao.getAllbyName(name, limit, page);
	}
}
